/**
 * 智慧小区管理系统 - 楼栋管理API接口
 * @author Wu.Liang
 * @date 2024-12-21
 * @updated 2025-01-30 - 添加下拉框专用接口，返回数组格式数据
 */
import api from './request'
import type { ApiResponse } from './request'

// 楼栋相关类型定义
export interface BuildingData {
  id: number
  buildingName: string
  buildingNo: string
  communityId: number
  communityName: string
  floorCount: number
  unitCount: number
  unitsPerFloor?: number
  totalHouseholds: number
  buildingType?: string
  status: number
  remark?: string
  undergroundFloor?: number
  structureType?: number
  buildYear?: string
  area?: number
  landArea?: number
  buildingHeight?: number
  hasElevator?: number
  elevatorCount?: number
  address?: string
  createTime: string
  updateTime: string
}

export interface CreateBuildingDTO {
  buildingName: string
  buildingNo: string
  communityId: number
  buildingType?: string
  floorCount: number
  unitCount: number
  unitsPerFloor?: number
  status: number
  remark?: string
  undergroundFloor?: number
  structureType?: number
  buildYear?: string
  area?: number
  landArea?: number
  buildingHeight?: number
  hasElevator?: number
  elevatorCount?: number
  address?: string
}

export interface UpdateBuildingDTO {
  buildingName?: string
  buildingNo?: string
  communityId?: number
  buildingType?: string
  floorCount?: number
  unitCount?: number
  unitsPerFloor?: number
  status?: number
  remark?: string
  undergroundFloor?: number
  structureType?: number
  buildYear?: string
  area?: number
  landArea?: number
  buildingHeight?: number
  hasElevator?: number
  elevatorCount?: number
  address?: string
}

export interface BuildingQueryDTO {
  buildingName?: string
  buildingNo?: string
  communityId?: number
  status?: number
  pageNum?: number
  pageSize?: number
}

export interface BuildingLayoutData {
  buildingId: number
  buildingName: string
  communityId: number
  floors: FloorData[]
}

export interface FloorData {
  floorNumber: number
  units: UnitData[]
}

export interface UnitData {
  id: number
  unitName: string
  households: HouseholdData[]
}

export interface HouseholdData {
  id: number
  roomNumber: string
  status: number
  area: number
  roomCount: number
  hallCount: number
}

// 楼栋API接口
export const buildingApi = {
  /**
   * 获取楼栋列表（分页）
   * @author Wu.Liang
   */
  getBuildingList: (params: BuildingQueryDTO) => 
    api.get<ApiResponse<any>>('/api/region/buildings', { params }),

  /**
   * 根据小区获取楼栋列表（下拉框专用，返回数组格式）
   * @author Wu.Liang
   * @updated 2025-01-30 - 新增下拉框专用接口
   */
  getBuildingsByCommunity: (communityId: number) => 
    api.get<ApiResponse<BuildingData[]>>(`/api/region/buildings/community/${communityId}`),

  /**
   * 获取楼栋分页列表
   * @author Wu.Liang
   */
  getBuildingPage: (params: BuildingQueryDTO) => 
    api.get<ApiResponse<any>>('/api/region/buildings', { params }),

  /**
   * 获取楼栋详情
   * @author Wu.Liang
   */
  getBuildingDetail: (id: number) => 
    api.get<ApiResponse<BuildingData>>(`/api/region/buildings/${id}`),

  /**
   * 创建楼栋
   * @author Wu.Liang
   */
  createBuilding: (data: CreateBuildingDTO) => 
    api.post<ApiResponse<void>>('/api/region/buildings', data),

  /**
   * 更新楼栋
   * @author Wu.Liang
   */
  updateBuilding: (id: number, data: UpdateBuildingDTO) => 
    api.put<ApiResponse<void>>(`/api/region/buildings/${id}`, data),

  /**
   * 删除楼栋
   * @author Wu.Liang
   */
  deleteBuilding: (id: number) => 
    api.delete<ApiResponse<void>>(`/api/region/buildings/${id}`),

  /**
   * 获取楼栋布局
   * @author Wu.Liang
   */
  getBuildingLayout: (id: number) => 
    api.get<ApiResponse<BuildingLayoutData>>(`/api/region/buildings/${id}/layout`),



  /**
   * 获取楼栋统计信息
   * @author Wu.Liang
   */
  getBuildingStatistics: (id: number) => 
    api.get<ApiResponse<any>>(`/api/region/buildings/${id}/statistics`)
} 